let vue = new Vue({
    el: '.page-content',
    data: function(){
        return {
            result:{},
            name:'',
            aid:'',
            condition:{
                type:'0',
                companyName:'全部'
            },
            // conditionName:'',
            setting:{
                data:{//设置树的数据的配置
                    simpleData: {//简单数据格式配置  一维结构节点数组 不设置默认是多维结构数组
                        enable:true,  //开启使用简单数据格式
                        // idKey: 'id', //设置节点中的id属性key  默认
                        pIdKey: 'parentId' //设置父节点id属性key   与数据库中的字段需要对应
                    }
                },edit:{
                    enable: true,  //开启编辑小图标，默认只有删除和修改
                    removeTitle: '删除',
                    renameTitle: '编辑'
                },
                callback:{
                    onClick:this.onClick2,
                    beforeEditName: this.beforeEditName,
                    beforeRemove: this.beforeRemove
                },
                view:{
                    addHoverDom:this.addHoverDom,
                    removeHoverDom:this.removeHoverDom
                }
            },
            nodes:[],
        }
    },
    methods: {
        beforeEditName:function (treeId,treeNode) {
            if(treeNode.id!=0 && treeNode.id!=1){
                this.toUpdate(treeNode.id);
            }
            return false;
        },
        beforeRemove:function (treeId,treeNode) {
            if(treeNode.id!=0 && treeNode.id!=1){
                this.deleteOne(treeNode.id);
                return;
            }
            return false;
        },
        addHoverDom:function (treeId,treeNode) {
            if (treeNode.id === 0) {
                return;
            }
            let span = $(`#${treeNode.tId}_add`);
            if(span.length>0){
                return;
            }
            span = `<span class="button add" id="${treeNode.tId}_add" title="添加" treenode_add="" style=""></span>`
            $(`#${treeNode.tId}_a`).append(span);
            $(`#${treeNode.tId}_add`).on('click',function () {
                vue.insertOne(treeNode.id)
            })
        },
        removeHoverDom: function (treeId,treeNode) {
            if (treeNode.id === 0) {
                return;
            }
            $(`#${treeNode.tId}_add`).off().remove();
        },
        onClick2:function (event, treeId, treeNode) {
            if (treeNode.id===0||treeNode.id===1){
                return;
            }
            this.name=treeNode.name;
            // $("#opneOrClose").removeClass('open')
        },
        initTree:function () {
            axios({
                url:'area/selectTree'
            }).then(response=>{
                this.nodes=response.data.obj;
                //动态生成父节点 全部
                this.nodes[this.nodes.length] = {id:0,name:'区域列表'};
                //初始化树(dom节点对象,配置对象,节点数组)
                let zTreeObj = $.fn.zTree.init($('#treeMenu'),this.setting,this.nodes);
                let nodes = zTreeObj.getNodes();//获取组装后的节点数组
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        select: function (pageNum, pageSize) {
            axios({
                url: `area/index/${pageNum}/${pageSize}`,
                method: 'post',
                data:{name:this.name,
                        }
            }).then(rs => {
                this.result = rs.data.obj;
            })
        },
        selectAll: function (pageNum, pageSize) {
            this.name='';
            this.aid='';
            axios({
                url: `area/index/${pageNum}/${pageSize}`,
                method: 'post',
                data:{}
            }).then(rs => {
                this.result = rs.data.obj;
            })
        },
        toUpdate: function (id) {
            layer.appId = id;
            layer.message = '';
            layer.open({
                type: 2,
                title: false,
                area: ['80%', '80%'],
                content: 'area/toUpdate',
                end: () => {
                    if (layer.message != '') {
                        layer.msg("更新成功");
                        this.initTree();
                        this.selectAll(1,5)
                    }
                }
            })
        },
        deleteOne: function (id) {
            axios({
                url: `area/deleteOne/${id}`,
            }).then(rs => {
                if (rs.data >= 1) {
                    layer.msg("删除成功");
                    this.initTree();
                    this.selectAll(1, 5)
                } else {
                    layer.msg("删除失败")
                }
            })
        },
        insertOne: function (id) {
            layer.id=id;
            layer.message='';
            layer.open({
                type:2,
                title: false,
                area: ['80%', '80%'],
                content: 'area/insertOne',
                end:()=>{
                    if (layer.message!=''){
                        layer.msg("添加成功");
                        this.initTree();
                        this.selectAll(1,5)
                    }
                }
            })
        },
        detailView: function (id) {
            layer.id=id;
            layer.message = '';
            layer.open({
                type: 2,
                title: false,
                area: ['80%', '80%'],
                content: 'area/detailView',
                end:()=>{
                }
            })
        },
        upload:function (event) {
            let file = event.target.files[0];
            let formData = new FormData();
            formData.append("file",file);
            axios({
                url:"area/upload",
                method:'post',
                headers:{
                    'content-type':'multipart/form-data'
                },
                data:formData
            }).then(rs=>{
                if (rs.data>0){
                    layer.msg("读取成功");
                    this.initTree();
                    this.selectAll(1,5)
                }
            }).catch(error=>{
                layer.msg(error.message)
            })
        },
        download:function () {
            location.href='area/download';
        }
    },
    created: function () {
        this.selectAll(1, 5);
        this.initTree();
    }
});